package com.rjgc.title.controller.user;

import com.rjgc.title.common.result.Result;
import com.rjgc.title.common.util.AuthContextHolder;
import com.rjgc.title.common.util.MD5;
import com.rjgc.title.model.entity.user.UserInfo;
import com.rjgc.title.service.UserInfoService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @program: title-parent
 * @author: Mr.Zhuo
 * @create: 2022-04-25 08:29
 **/
@Api(tags = "用户信息")
@RestController
@RequestMapping("/api/user")
public class UserApiController {

    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("inner/findUserInfoByUserId")
    public Result findUserInfoByUserId(HttpServletRequest request) {
        // 获取当前对象的id
        String userId = (String) request.getSession().getAttribute("userId");
        // 获取当前对象详细信息
        UserInfo userInfo = userInfoService.findUserInfo(userId);

        return Result.ok(userInfo);
    }

    @PostMapping("inner/saveUserInfo")
    public Result saveUserInfo(HttpServletRequest request, @RequestBody UserInfo userInfo) {
        // 获取当前对象的id
        String userId = (String) request.getSession().getAttribute("userId");
        userInfo.setId(Long.parseLong(userId));
        // 保存当前用户
        userInfoService.saveUserInfo(userInfo);

        // 返回前端数据
        return Result.ok();
    }

    /**
     * 修改密码
     * @param request
     * @param map
     * @return
     */
    @GetMapping("inner/updatePass")
    public Result updatePass(HttpServletRequest request, @RequestBody Map map) {
        // 获取当前对象的id
        String userId = (String) request.getSession().getAttribute("userId");
        UserInfo userInfo = userInfoService.findUserInfo(userId);

        String oPasswordD = (String) map.get("oPassword");
        String oPassword = MD5.encrypt(oPasswordD);

        String nPasswordD = (String) map.get("nPassword");
        String nPassword = MD5.encrypt(nPasswordD);
        // 修改密码
        Boolean flag = userInfoService.updatePass(userInfo, oPassword, nPassword);

        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


}
